package com.atguigu.commonutils.result;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;

import java.io.Serializable;
import java.util.List;

public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    //总记录数
    private int total;
    //每页记录数
    private int pageSize;
    //总页数
    private int totalPage;
    //当前页数
    private int currentPage;
    //列表数据
    private List<T> list;

    /**
     * 分页
     * @param list        列表数据
     * @param total  总记录数
     * @param pageSize    每页记录数
     * @param currentPage    当前页数
     */
    public PageResult(List<T> list, int total, int pageSize, int currentPage) {
        this.list = list;
        this.total = total;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalPage = (int)Math.ceil((double) total /pageSize);
    }

    /**
     * 分页
     */
    public PageResult(PageInfo<T> page) {
        this.list = page.getList();
        this.total = (int)page.getTotal();
        this.pageSize = page.getSize();
        this.currentPage = page.getPageNum();
        this.totalPage = page.getPages();
    }

    /**
     * 分页
     */
    public PageResult(Page<T> page) {
        this.list = page.getRecords();
        this.total = (int)page.getTotal();
        this.pageSize = (int) page.getSize();
        this.currentPage = (int) page.getCurrent();
        this.totalPage = (int)page.getPages();
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
